<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/adminbase.php' );

// --

class pegawai extends adminbase {

    //constructor
    public function pegawai() {
        parent::__construct();
        //load librray notifikasi
        $this->load->library('tnotification');
        //load library pagination
        $this->load->library('pagination');
        //load model
        $this->load->model('pegawai/m_pegawai');
    }
    public function index() {
        $this->page();
    }
    function page() {
        //display pagination
        $strTotalRow = $this->m_pegawai->count();
        $strConfig['base_url'] = site_url("pegawai/pegawai/page");
        $strConfig['total_rows'] = $strTotalRow;
        $strConfig['per_page'] = 20;
        $strConfig['uri_segment'] = 4;
        $strConfig['cur_tag_open'] = "<a class='current'>";
        $strConfig['cur_tag_close'] = '</a>';
        $this->pagination->initialize($strConfig);
        $strPagination = $this->pagination->create_links();
        $strPage = $this->uri->segment(4);

        $strLimit = $strConfig['per_page'];
        if (!$strPage) {
            $strPage = 0;
        }
        // bisnis proses
        $arrParams = array(intval($strPage), $strLimit);
        $strData = $this->m_pegawai->getAllCode($arrParams);
        //url
        $this->smarty->assign("base_url", site_url());
        $this->smarty->assign("strUrllist", site_url("pegawai/pegawai"));
        $this->smarty->assign("strUrladd", site_url("pegawai/pegawai/add"));
        $this->smarty->assign("strUrldel", site_url("pegawai/pegawai/processDelete"));
        $this->smarty->assign("strUrledit", site_url("pegawai/pegawai/edit"));
        $this->smarty->assign("strUrldetail", site_url("pegawai/pegawai/detail"));
        
        //url sidebar
        $this->smarty->assign("strUrlAdmin", site_url("admin/admin"));
        $this->smarty->assign("strUrlDokter", site_url("dokter/dokter"));
        $this->smarty->assign("strUrlPegawai", site_url("pegawai/pegawai"));
        $this->smarty->assign("strUrlPasien", site_url("pasien/pasien"));
        $this->smarty->assign("strUrlJenisObat", site_url("jenisobat/jenisobat"));
        $this->smarty->assign("strUrlObat", site_url("obat/obat"));
        $this->smarty->assign("strUrlPoli", site_url("poli/poli"));
        
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        //set breadcrumb
        $strBreadcrumbHtml = "<a href='" . base_url() . "index.php/home/welcomeadmin'>Home </a>&raquo; <b>pegawai</b>";
        $this->smarty->assign("strBreadcrumbUrl", $strBreadcrumbHtml);

        //assign variable
        $this->smarty->assign("strPegawaiData", $strData);
        $this->smarty->assign("strPagination", $strPagination);
        // set template content
        $this->smarty->assign("strTemplateContent", "pegawai/list.html");
        // output
        parent::display();
    }

    public function add() {
        // set template content
        $this->smarty->assign("strTemplateContent", "pegawai/add.html");
  
        //url tab
        $this->smarty->assign("base_url", site_url());
        $this->smarty->assign("strUrllist", site_url("pegawai/pegawai"));
        $this->smarty->assign("strUrladd", site_url("pegawai/pegawai/add"));
        $this->smarty->assign("strUrlsave", site_url("pegawai/pegawai/processAdd"));
        $this->smarty->assign("strUrldetail", site_url("pegawai/pegawai/detail"));

         //url sidebar
        $this->smarty->assign("strUrlAdmin", site_url("admin/admin"));
        $this->smarty->assign("strUrlDokter", site_url("dokter/dokter"));
        $this->smarty->assign("strUrlPegawai", site_url("pegawai/pegawai"));
        $this->smarty->assign("strUrlPasien", site_url("pasien/pasien"));
        $this->smarty->assign("strUrlJenisObat", site_url("jenisobat/jenisobat"));
        $this->smarty->assign("strUrlObat", site_url("obat/obat"));
        $this->smarty->assign("strUrlPoli", site_url("poli/poli"));
        
        //set breadcrumb
        $strBreadcrumbHtml = "<a href='" . base_url() . "index.php/home/welcomeadmin'>Home </a>&raquo; <a href='" . base_url() .
                "index.php/pegawai/pegawai'>pegawai </a>&raquo; <b> Tambah</b>";
        $this->smarty->assign("strBreadcrumbUrl", $strBreadcrumbHtml);
        //display notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function processAdd() {
        $this->tnotification->set_rules('name', 'Nama pegawai', 'trim|required|max_length[20]');
        $this->tnotification->set_rules('username', 'Username', 'trim|required|max_length[20]');
        $this->tnotification->set_rules('password', 'Password', 'trim|required|max_length[20]');
//        $this->tnotification->set_rules('jenis_kelamin', 'Jenis Kelamin', 'trim|required');
//        $this->tnotification->set_rules('kota_lahir', 'Kota Lahir', 'trim|required');
//        $this->tnotification->set_rules('tgl_lahir', 'Tgl Lahir', 'trim|required');
//        $this->tnotification->set_rules('telp', 'Telp', 'trim|required');
//        $this->tnotification->set_rules('alamat', 'Alamat', 'trim|required');
//        $this->tnotification->set_rules('tgl_masuk', 'Tgl Masuk', 'trim|required');
        $this->tnotification->set_rules('jabatan', 'Jabatan', 'trim|required');
        if ($this->tnotification->run() != FALSE) {
            $strSessData = $this->session->userdata('session_admin');
            $arrParam = array($this->input->post('name'),
                $this->input->post('username'),
                $this->input->post('password'),
                $this->input->post('jenis_kelamin'),
                $this->input->post('kota_lahir'),
                $this->input->post('tgl_lahir'),
                $this->input->post('telp'),
                $this->input->post('alamat'),
                $this->input->post('tgl_masuk'),
                $this->input->post('foto'),
                $this->input->post('jabatan'));
            if (($this->m_pegawai->saveAdd($arrParam))) {// == TRUE
                //display success notif
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        redirect('pegawai/pegawai/add');
    }
    public function detail($pPegawai) {
       
        $arrPegawaiId = $this->m_pegawai->getById($pPegawai);
        $arrPegawai =  $this->m_pegawai->getAll();
        $this->smarty->assign("arrpegawai", $arrPegawaiId);   
       
         //jenis kelamin
        $strHtmlJkList = "";
        $arrJenisKelaminList = array('laki'=>'L','pere'=>'P');
        foreach ($arrJenisKelaminList as $arrJkList) {
             if ($arrJkList == $arrPegawaiId['jenis_kelamin']) {
                 if($arrPegawaiId['jenis_kelamin'] == 'L'){
                       $strHtmlJkList .= "<option value='" . $arrJkList['laki'] . "' selected='selected'>Laki-laki</option>";
                       $strHtmlJkList .= "<option value='" . $arrJkList['pere'] . "'>Perempuan</option>";
                      }
                 else{
                      $strHtmlJkList .= "<option value='" . $arrJkList['laki'] . "'>Laki-laki</option>";
                      $strHtmlJkList .= "<option value='" . $arrJkList['pere'] . "' selected='selected'>Perempuan</option>";
                 }
            } 
        }
          //jenis kelamin
        $strHtmlStList = "";
        $arrJenisKelaminList = array('laki'=>'L','pere'=>'P');
        foreach ($arrPegawai as $arrP) {
            if ($arrP['jenis_kelamin'] == $arrPegawaiId['jenis_kelamin']) {
                $strHtmlStList .= "<option value='" . $arrP['jenis_kelamin'] . "' selected='selected'>'" . $arrP['jenis_kelamin'] . "'</option>";
            } 
            else{
                $strHtmlStList .= "<option value='" . $arrP['jenis_kelamin'] . "'>'" . $arrP['jenis_kelamin'] . "'</option>";
            }
        }
         //url sidebar
        $this->smarty->assign("strUrlAdmin", site_url("admin/admin"));
        $this->smarty->assign("strUrlDokter", site_url("dokter/dokter"));
        $this->smarty->assign("strUrlPegawai", site_url("pegawai/pegawai"));
        $this->smarty->assign("strUrlPasien", site_url("pasien/pasien"));
        $this->smarty->assign("strUrlJenisObat", site_url("jenisobat/jenisobat"));
        $this->smarty->assign("strUrlObat", site_url("obat/obat"));
        $this->smarty->assign("strUrlPoli", site_url("poli/poli"));
        
        $this->smarty->assign("strUrllist", site_url("pegawai/pegawai"));
        $this->smarty->assign("strUrladd", site_url("pegawai/pegawai/add"));
        $this->smarty->assign("strUrledit", site_url("pegawai/pegawai/editpegawai"));
        $this->smarty->assign("strUrldel", site_url("pegawai/pegawai/processDelete"));
        
        $this->smarty->assign("arrPegawaiData", $arrPegawaiId);
        $this->smarty->assign("strHtmlStList", $strHtmlStList);
        $this->smarty->assign("strHtmlJkList", $strHtmlJkList);
        
        $strBreadcrumbHtml = "<a href='" . base_url() . "index.php/home/welcomeadmin'>Home </a>&raquo; <a href='" . base_url() . "index.php/pegawai/pegawai'>pegawai</a>&raquo; <b>Detail</b>";
        $this->smarty->assign("strBreadcrumbUrl", $strBreadcrumbHtml);
        $this->smarty->assign("strTemplateContent", "pegawai/detail.html");
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        //-- set nav active menu
        parent::display();
    }
    public function edit($pStrId) {
        // set template content
        $this->smarty->assign("strTemplateContent", "pegawai/edit.html");

        $strPegawai = $this->m_pegawai->getById($pStrId);
        $strpegawaiList = $this->m_pegawai->getAll();
        //jenis kelamin
        $strHtmlJkList = "";
        $arrJenisKelaminList = array('laki'=>'L','pere'=>'P');
        foreach ($arrJenisKelaminList as $arrJkList) {
             if ($arrJkList == $strPegawai['jenis_kelamin']) {
                 if($strPegawai['jenis_kelamin'] == 'L'){
                       $strHtmlJkList .= "<option value='" . $arrJkList['laki'] . "' selected='selected'>Laki-laki</option>";
                       $strHtmlJkList .= "<option value='" . $arrJkList['pere'] . "'>Perempuan</option>";
                      }
                 else{
                      $strHtmlJkList .= "<option value='" . $arrJkList['laki'] . "'>Laki-laki</option>";
                      $strHtmlJkList .= "<option value='" . $arrJkList['pere'] . "' selected='selected'>Perempuan</option>";
                 }
            } 
        }
         //jabatan
        $strHtmlJbtList = "";
        $arrJabatan = array('pegPoli'=>'Pegawai Poli','adObat'=>'Administrasi Obat');
        
        foreach ($arrJabatan as $arrJab) {
           
             if ($arrJab == $strPegawai['st_jabatan']) {
                 if($strPegawai['st_jabatan'] == 'Pegawai Poli'){  
                       $strHtmlJbtList .= "<option value='" . $arrJabatan['pegPoli'] . "' selected='selected' >Pegawai Poli</option>";
                       $strHtmlJbtList .= "<option value='" . $arrJabatan['adObat'] . "'>Administrasi Obat</option>";
                      }
                 else{
                      $strHtmlJbtList .= "<option value='" . $arrJabatan['pegPoli'] . "'>Pegawai Poli</option>";
                      $strHtmlJbtList .= "<option value='" . $arrJabatan['adObat'] . "' selected='selected'>Administrasi Obat</option>";
                 }
                 
            } 
        }
        
        //url tab
        $this->smarty->assign("base_url", site_url());
        $this->smarty->assign("strUrllist", site_url("pegawai/pegawai"));
        $this->smarty->assign("strUrladd", site_url("pegawai/pegawai/add"));
        $this->smarty->assign("strUrlsave", site_url("pegawai/pegawai/processEdit"));
        //assign role data
        $this->smarty->assign("strPegawaiData", $strPegawai);
        $this->smarty->assign("strHtmlJkList", $strHtmlJkList);
        $this->smarty->assign("strHtmlJbtList", $strHtmlJbtList);
        //display notif
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        //set breadcrumb
        $strBreadcrumbHtml = "<a href='" . base_url() . "index.php/home/welcomeadmin'>Home </a>&raquo; <a href='" . base_url() .
                "index.php/pegawai/pegawai'>pegawai </a>&raquo; <b> Edit pegawai</b>";
        $this->smarty->assign("strBreadcrumbUrl", $strBreadcrumbHtml);
        // output
        parent::display();
    }
    public function processEdit() {
      $this->tnotification->set_rules('name', 'Nama pegawai', 'trim|required|max_length[20]');
      $this->tnotification->set_rules('username', 'Username', 'trim|required|max_length[20]');
      $this->tnotification->set_rules('password', 'Password', 'trim|required|max_length[20]');
//        $this->tnotification->set_rules('jenis_kelamin', 'Jenis Kelamin', 'trim|required');
//        $this->tnotification->set_rules('kota_lahir', 'Kota Lahir', 'trim|required');
//        $this->tnotification->set_rules('tgl_lahir', 'Tgl Lahir', 'trim|required');
//        $this->tnotification->set_rules('telp', 'Telp', 'trim|required');
//        $this->tnotification->set_rules('alamat', 'Alamat', 'trim|required');
//        $this->tnotification->set_rules('tgl_masuk', 'Tgl Masuk', 'trim|required');
     $this->tnotification->set_rules('jabatan', 'Jabatan', 'trim|required');

        if ($this->tnotification->run() != FALSE) {
            $strSessData = $this->session->userdata('session_admin');
             $arrParam = array($this->input->post('name'),
                $this->input->post('username'),
                $this->input->post('password'),
                $this->input->post('jenis_kelamin'),
                $this->input->post('kota_lahir'),
                $this->input->post('tgl_lahir'),
                $this->input->post('telp'),
                $this->input->post('alamat'),
                $this->input->post('tgl_masuk'),
                $this->input->post('foto'),
                $this->input->post('jabatan'),
                $this->input->post('id_pegawai'));
         
            if ($this->m_pegawai->saveEdit($arrParam)) {
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil diubah");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal diubah");
            }
        } else {
            $this->tnotification->sent_notification("error", "Data gagal diubah");
        }
        redirect('pegawai/pegawai/edit/' . $this->input->post('id_pegawai'));
    }
    public function processDelete() {
        //check rule
        $this->tnotification->set_rules('id_pegawai[]', 'Kode', 'trim|required');
        $strData = $this->input->post('id_pegawai');
        if ($this->tnotification->run() != FALSE) {
            foreach ($strData as $strVal) {
                // bisnis proses 
                if ($this->m_pegawai->delete($strVal)) {
                    $this->tnotification->sent_notification("success", "Data berhasil dihapus");
                } else {
                    $this->tnotification->sent_notification("error", "Data gagal dihapus");
                }
            }
        } else {
            $this->tnotification->sent_notification("error", "Silakan pilih data yang akan dihapus terlebih dahulu");
        }
        redirect('pegawai/pegawai/');
    }
}
?>